package com.kuang;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TestA {
    int n;

    public Integer getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }

    public TestA(int n) {
        this.n = n;
    }

    Integer getSum(TestA na) {
        if (na.getN() == 1) {
            return na.getN();
        } else {
            return na.getN() + getSum(new TestA(na.getN() - 1));
        }
    }

    public void sort1(int[] arr) {
        int ic = 0, iq = 0;
        boolean flag = true;
        for (int i = 0; i < arr.length - 1; ++i) {
            for (int j = 0; j < arr.length - 1 - i; ++j) {
                if (arr[j] < arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    ic++;
                    flag = false;
                }
                iq++;
            }
            if (flag) break;
            else flag = true;
        }
        System.out.println("交换次数为："+ic + "\t"+ "循环次数为:" + iq);
    }

    public void sort2(int[] arr) {
        int iq = 0, ic = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            int max = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[max] < arr[j]) {
                    max = j;
                }
                ic++;
            }
            if (arr[max] != arr[i]) {
                int temp = arr[max];
                arr[max] = arr[i];
                arr[i] = temp;
                iq++;
            }
        }
        System.out.println("交换次数为："+iq + "\t"+ "循环次数为:" + ic);
    }


    public static void main1(String[] args) {
        int[] arr1 = {2, 4, 42, 34, 3, 123};
        int[] arr2 = {2, 4, 42, 34, 3, 123};
        TestA testA = new TestA(5);
        testA.sort1(arr1);
        testA.sort2(arr2);
        for (int i : arr1) {
            System.out.print(i + "\t");
        }
        System.out.println();
        for (int i : arr2) {
            System.out.print(i + "\t");
        }


    }


    public static void main(String[] args) {

        //数据库连接
        Connection connection =null;
        //预编译的Statement,使用这种预编译的statement可以提高数据库的性能，还能防止sql注入
        PreparedStatement preparedStatement =null;
        //结果集
        ResultSet resultSet =null;

        try{
            //加载数据库的驱动，这里使用mysql
            Class.forName("com.mysql.cj.jdbc.Driver");
            //通过驱动管理器来获得数据库的连接
            connection= DriverManager.getConnection("jdbc:mysql://47.100.182.185:3306/Eliteweb","root","Sr135595!");
            //定义sql语句
            String  sql="select * from student";
            //获取预处理的statement
            preparedStatement=connection.prepareStatement(sql);
            //设置占位符的参数
            //preparedStatement.setString(1,"zcj");
            //向数据库发出sql进行查询，查询出结果集
            resultSet=preparedStatement.executeQuery();
            //遍历结果集
            while(resultSet.next()){
                System.out.print(resultSet.getString("studentID"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放资源,注意倒着释放
            if(resultSet!=null){
                try{
                    resultSet.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(preparedStatement!=null){
                try{
                    preparedStatement.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            if(connection!=null){
                try{
                    connection.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

}
